<?php
/**
 * Created by PhpStorm.
 * User: matengfei
 * Date: 15/12/6
 * Time: 下午8:49
 */

<
!--未成功 -->
$Pass = "123456";
$Clear = "abcdef";

$crypted = mc_encrypt($Clear, $Pass);
echo "Encrypred: " . $crypted . "</br>";

$newClear = mc_decrypt($crypted, $Pass);
echo "Decrypred: " . $newClear . "</br>";



function mc_encrypt($encrypt, $mc_key)
{

    $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_DES, MCRYPT_MODE_CBC), MCRYPT_RAND);
    $passcrypt = trim(mcrypt_encrypt(MCRYPT_DES, $mc_key, trim($encrypt), MCRYPT_MODE_CBC, $iv));
    $encode = base64_encode($passcrypt); //$encode = $passcrypt; //
    return $encode;
}

function mc_decrypt($decrypt, $mc_key)
{
    //$decoded = $decrypt;
    $decoded = base64_decode($decrypt);
    $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_DES, MCRYPT_MODE_CBC), MCRYPT_RAND);
    $decrypted = trim(mcrypt_decrypt(MCRYPT_DES, $mc_key, trim($decoded), MCRYPT_MODE_CBC, $iv));
    return $decrypted;
}

?>